﻿//----------------------------------------------------------------------------------------------------
// The LevelFileInterface is used for saving and laoding of levels
//----------------------------------------------------------------------------------------------------

package HG.TypeInterfaces
{
	import flash.events.*;
	import flash.utils.ByteArray;

	import HG.GameDataTypes.ProfileStore
	import HG.GameDataTypes.Profile;
	
	import HG.Debug;
	import HG.Managers.ProfileManager;
	
	public class ProfileStoreFileInterface extends IFileInterface
	{
		//----------------------------------------------------------------------------------------------------
		public function ProfileStoreFileInterface(fileName:String)
		{
			SetFile(fileName);
		}
		
		//----------------------------------------------------------------------------------------------------
		public function SaveStore(store:ProfileStore)
		{
			var bytes:ByteArray = new ByteArray();
			bytes.writeObject(store);
			Save(bytes);
		}
		
		//----------------------------------------------------------------------------------------------------
		public function LoadStore(callback:Function)
		{
			Debug.ASSERT(m_file, "Attempted to load null file");
			
			m_fileReference.browse();
			m_fileReference.addEventListener(Event.SELECT, OnBeginLoad);
			m_fileReference.addEventListener(Event.COMPLETE, OnLoadComplete);
			
			m_callback = callback;
		}
		
		//----------------------------------------------------------------------------------------------------
		protected function OnBeginLoad(evt:Event):void 
		{
			m_fileReference.load()
		}
		
		//----------------------------------------------------------------------------------------------------
		protected override function OnLoadComplete(evt:Event):void 
		{
			m_data = m_fileReference.data;
			var store:ProfileStore;
			m_loaded = true;
			
			try
			{
				store = new ProfileStore();
				//var profile = new Profile("init_profile");
				store = m_data.readObject() as ProfileStore;
			}
			catch(ex)
			{
				store = null;
			}

			m_callback(store);
		}
		//----------------------------------------------------------------------------------------------------

	}

}